angular.module('app').controller('moreCtrl', ['$scope', '$http', 'userInfo', '$state', 'utils', function($scope, $http, userInfo, $state, utils) {

	$scope.phone = userInfo.phone //设置手机号
	$scope.seqnumber = null //短信流水号
	$scope.applyid = null //绑卡申请号
	/*弹出客服电话*/
	$scope.showTel = function() {
		mui.confirm('400-800-5818', ' ', ['取消', '呼叫'], function(e) {
			if(e.index === 1) {
				window.location.href = 'tel://4008005818#mp.weixin.qq.com';
			}
		}, 'div')
	}

	/*意见反馈*/
	$scope.feedback = function() {
		$http.get(utils.accountSys, {
			params: {
				header: utils.header("203201"),
				body: {
					context: $scope.content,
					os_version: "微信端",
					phone: $scope.lxPhone,
					soft_version: "微信端",
					user_code: userInfo.user_code
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				utils.msg("提交成功!")
				$scope.content=""
			}
		})

	}

	/*修改密码*/
	$scope.ModifyPassword = function(category) {
		$scope.category = category

		mui('#ModifyPassword').popover('toggle');
		$('#ModifyPassword').before($('.mui-backdrop'))
	}
	/*关闭弹窗*/
	$scope.close = function() {
		mui('#ModifyPassword').popover('toggle');
		$('.mui-backdrop').remove()
	}
	/*点击遮罩删除遮罩,因为和mui遮罩有兼容问题需要这样删除*/
	$(document).on('click', '.mui-backdrop', function() {
		$('.mui-backdrop').remove()
	})

	//个人中心头像点击
	$scope.check_login = function() {
		if(userInfo.user_code) {
			$state.go("usercore")
		} else {
			$state.go("register")
		}
	}

	/*退出*/
	$scope.SignOut = function() {
		if(userInfo.user_code) {
			mui.confirm('是否退出登录', ' ', ['取消', '确定'], function(e) {
				if(e.index === 1) {
					userInfo.user_code = null //清空用户
					localStorage.clear() //清空本地缓存
					window.location.reload()
				}
			}, 'div')

		} else {

			$state.go("register")

		}
	}

	/*显示手机号*/
	$scope.phone = userInfo.phone //获取手机号
	/*个人中心*/
	if($state.current.name == "usercore" || $state.current.name == "userbank" || $state.current.name == "EditPhone" || $state.current.name == "ValidatePhone" || $state.current.name == "validateRealname"||$state.current.name == "editBank") {
		if(userInfo.user_code) {
			userInfo.getUserInfo().then(function(res) {
				$scope.userState = res //用户状态
				if(res.is_real_name != 0 || res.is_bind_card != 0) {
					var promise = userInfo.getUserIdentity()
					promise.then(function(res) {
						$scope.userinfo = res[0]
					})
					userInfo.getUserBank().then(function(res) {
						angular.extend($scope.userinfo, res.body)
					})
				}
			})

		}
	}

	/*意见反馈*/
	if($state.current.name == "feedback") {

		$scope.lxPhone = userInfo.phone

	}

	/*银行卡列表*/
	if($state.current.name == "bankList") {

		$http.get(utils.paySys, {
			params: {
				header: utils.header("020027"),
				body: {}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				$scope.bankList = res.body
			}
		})

	}

	/*找回登录密码*/
	$scope.sendLoginPwd = function() {
		$http.get(utils.userSys, {
			params: {
				header: utils.header("070053"),
				body: {
					generate_seq: $scope.seqnumber,
					verification_code: $scope.code
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				userInfo.backPwd = true //设置重置密码状态
				if(userInfo.user_code) {
					userInfo.user_code_temp = userInfo.user_code
				}
				$state.go("SetLoginPassword")
			}

		})
	}

	/*找回交易密码*/
	$scope.sendTradePwd = function() {
		$http.get(utils.userSys, {
			params: {
				header: utils.header("070053"),
				body: {
					generate_seq: $scope.seqnumber,
					verification_code: $scope.code
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				userInfo.backPwd = true //设置重置密码状态
				$state.go("SetTradePassword")
			}

		})

	}

	/*修改密码*/
	$scope.EditLoginPwd = function() {
		$http.get(utils.userSys, {
			params: {
				header: utils.header("070055"),
				body: {
					login_type: "0",
					new_password: md5($scope.LoginPassword),
					old_password: md5($scope.oldPwd),
					set_type: "1",
					user_code: userInfo.user_code,
					user_type: "0",
					fingerprint_password: "",
					gesture_password: "",
					third_bind_id: "",
					third_platform_id: ""
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				utils.msg(res.header.re_msg, 2, function() {
					window.history.go(-1)
				})
			}
		})
	}

	/*修改交易密码*/
	$scope.EditTradePwd = function() {
		$http.get(utils.userSys, {
			params: {
				header: utils.header("070115"),
				body: {
					new_password: md5($scope.LoginPassword),
					ori_password: md5($scope.oldPwd),
					type: "1",
					user_code: userInfo.user_code
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				utils.msg(res.header.re_msg, 2, function() {
					window.history.go(-1)
				})
			}
		})

	}

	/*修改预留手机号*/
	$scope.EditPhone = function() {
		//验证交易密码
		$http.get(utils.userSys, {
				params: {
					header: utils.header("070114"),
					body: {
						user_code: userInfo.user_code,
						tra_password: md5($scope.pwd)
					}
				}
			})
			.then(function(res) {
				if(res.header.re_code == "0000") {
					return $http.get(utils.userSys, {
						params: {
							header: utils.header("070125"),
							body: {
								unbind_flag: 3,
								user_code: userInfo.user_code
							}
						}
					})

				} else {
					throw 0
				}

			})
			/*解绑银行卡*/
			.then(function(res) {
				if(res.header.re_code == "0000") {
					$state.go("ValidatePhone")
				}

			})
			.catch(function() {

			})

	}

	/* 更换银行卡判断*/

	$scope.editBank = function() {
		/*获取用户资产*/
		$http.post(utils.balanceSys, {}, {
			params: {
				header: utils.header("000012"),
				body: {
					user_code: userInfo.user_code
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				if(Number(res.body.assetsCurrentTotal) >= 1) {
					mui.confirm('您的总资产(含提现中)超过1元,需联系客服进行人工换卡，客服电话400-800-5818', ' ', ['以后再说', '联系客服'], function(e) {
						if(e.index === 1) {
							window.location.href = 'tel://4008005818#mp.weixin.qq.com';
						}
					}, 'div')
				} else {

					$state.go("editBank")

				}
			}
		})

	}

	/*更换银行卡*/
	$scope.editBankSend = function() {

		$http.get(utils.userSys, {
			params: {
				header: utils.header("070130"),
				body: {
					id_number: $scope.realName,
					id_type: 0,
					login_password: "",
					password_trade: md5($scope.LoginPassword),
					password_type: 2,
					phone_number: userInfo.phone,
					real_name: $scope.userinfo.real_name

				}
			}
		}).then(function(res) {
			/*身份信息验证成功 */
			if(res.header.re_code == "0000") {

				userInfo.getUserInfo().then(function(res) {
					$http.get(utils.userSys, {
						params: {
							header: utils.header("070125"),
							body: {
								current_status: res.card_bind_status,
								unbind_flag: 1,
								user_code: userInfo.user_code
							}
						}
					}).then(function(res) {
						if(res.header.re_code == "0000") {
							$state.go("BindBank")
						}

					})

				})

			}

		})

	}

	/*验证预留手机号*/
	$scope.BindBank = function() {
		$http.get(utils.userSys, {
			params: {
				header: utils.header("070090"),
				body: {
					application_number: $scope.applyid,
					sms_verify: $scope.code,
					user_code: userInfo.user_code
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				utils.msg("验证成功!", 1, function() {
					history.go(-1)
				})

			}
		})
	}

	/*验证身份*/
	$scope.validateRealname = function() {

		$http.get(utils.userSys, {
			params: {
				header: utils.header("070130"),
				body: {
					id_number: $scope.realName,
					id_type: 0,
					login_password: md5($scope.LoginPassword),
					phone_number: $scope.userinfo.phone_number,
					real_name: $scope.userinfo.real_name,
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				$state.go("BindNew")
			}

		})

	}

	/*绑定新号*/
	$scope.BindNew = function() {
		/*校验验证码*/
		$http.get(utils.userSys, {
			params: {
				header: utils.header("070053"),
				body: {
					generate_seq: $scope.seqnumber,
					verification_code: $scope.code,
					phone_number: $scope.newPhone
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				$http.get(utils.userSys, {
					params: {
						header: utils.header("070057"),
						body: {
							change_type: 0,
							new_phone_number: $scope.newPhone,
							old_phone_number: $scope.phone
						}
					}
				}).then(function(res) {
					if(res.header.re_code == "0000") {

						utils.msg("设置成功!", 1, function() {
							$state.go("user")
						})

					}

				})

			}

		})

	}

	/*银行卡页面点击修改预留手机号*/
	$scope.modifyPhone = function() {

		$http.get(utils.userSys, {
			params: {
				header: utils.header("070118"),
				body: {
					user_code: userInfo.user_code,
					acc_id: $scope.userinfo.card.acc_id
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				/*有提现中金额*/
				if(res.body.withdraw_amt > 0) {
					return mui.alert("您有提现未到账资金，该操作可能会影响提现的正常到账，请待资金到账后再修改预留手机号。", "", "我知道了")
				}

				if($scope.userState.card_bind_status == 3) {
					$state.go("ValidatePhone")
				} else {
					$state.go("EditPhone")
				}

			}

		})

	}

}])